using System;
using System.IO;
using System.Drawing;

namespace PIM.Utilities.Cryptography
{
	/// <summary>
	/// Purpose : This class shall have methods that shall support 
	/// encoding/decoding for different types of conversion.
	/// </summary>
	public class QSecureEncoding
	{
        System.Drawing.Imaging.ImageFormat format;
		public QSecureEncoding()
		{
			//
			// TODO: Add constructor logic here
			//
		}
        public string ImageToBase64(Image image,System.Drawing.Imaging.ImageFormat format)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                image.Save(ms, format);
                byte[] imageBytes = ms.ToArray();

                string base64String = Convert.ToBase64String(imageBytes);
                return base64String;
            }
        }

        public string ImageToBase64(string fileName,byte[]fileByte, string fileExtension)
        {
            bool isValidFileType = false;
            string base64String = "";
            System.Drawing.Image newImage;
            string[] strExtension = new string[] { ".gif", ".jpg", ".bmp", ".emf", ".exif", ".icon", ".memorybmp", ".png", ".tiff", ".wmf" };
            foreach (string strExtensionType in strExtension)
            {
                if (strExtensionType == fileExtension.ToLower())
                {
                    isValidFileType = true;
                    break;
                }
            }

            switch (fileExtension.ToLower())
            {
                case ".gif":
                    format = System.Drawing.Imaging.ImageFormat.Gif;
                    break;
                case ".jpg":
                    format = System.Drawing.Imaging.ImageFormat.Jpeg;
                    break;
                case ".bmp":
                    format = System.Drawing.Imaging.ImageFormat.Bmp;
                    break;
                case ".emf":
                    format = System.Drawing.Imaging.ImageFormat.Emf;
                    break;
                case ".exif":
                    format = System.Drawing.Imaging.ImageFormat.Exif;
                    break;
                case ".icon":
                    format = System.Drawing.Imaging.ImageFormat.Icon;
                    break;
                case ".memorybmp":
                    format = System.Drawing.Imaging.ImageFormat.MemoryBmp;
                    break;
                case ".png":
                    format = System.Drawing.Imaging.ImageFormat.Png;
                    break;
                case ".tiff":
                    format = System.Drawing.Imaging.ImageFormat.Tiff;
                    break;
                case ".wmf":
                    format = System.Drawing.Imaging.ImageFormat.Wmf;
                    break;
                default:
                    break;
            }

            if (isValidFileType)
            {
                using (MemoryStream stream = new MemoryStream(fileByte))
                {
                    newImage = System.Drawing.Image.FromStream(stream);
                    using (MemoryStream ms = new MemoryStream())
                    {
                        newImage.Save(ms, format);
                        byte[] imageBytes = ms.ToArray();

                        base64String = Convert.ToBase64String(imageBytes);

                    }
                }
            }
            return base64String;
        }

        public Image Base64ToImage(string base64String)
        {
            byte[] imageBytes = Convert.FromBase64String(base64String);
            MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);

            ms.Write(imageBytes, 0, imageBytes.Length);
            Image image = Image.FromStream(ms, true);
            return image;
        }
	}
}
